﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Sys_commonuser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindddl();
            bindGridView(true);
        }
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bindGridView(false);
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string tid = e.CommandArgument.ToString().Trim();
        PM.PMSys.account teacher = new PM.PMSys.account();
        if (e.CommandName == "del")
        {
            int result = teacher.Delete_Account(tid);
            if (result > 0)
                bindGridView(false);
            else
                PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "删除失败！");
        }
        if (e.CommandName == "reset")
        {
            string salt = PM.Code.Web.safety.get_GenerateSalt();//重新生成密码加密权值
            string pwd = PM.Code.Web.safety.EncodePassword("123456", salt);//重置密码为123456
            int result = teacher.ResetPwd(tid, pwd, salt);
            if (result > 0)
            {
                PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "重置密码成功，新密码为123456！");
            }
            else
                PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "密码重置失败！");
        }
    }
    protected void bindGridView(bool GetCount)
    {
        //绑定gridview
        StringBuilder sqlstrfield = new StringBuilder();
        StringBuilder sqlstrwhere = new StringBuilder();
        getField(ref sqlstrfield);
        getWhere(ref sqlstrwhere);
        PM.Code.Web.controlbind.BindGridViewPublicProc(this.GridView1, this.AspNetPager1, "Sys_Account", "id", sqlstrfield.ToString(), "id", sqlstrwhere.ToString(), GetCount);
    }
    protected void bindddl()
    {
        //绑定学院的下拉框
        PM.Code.Web.controlbind.BindDropDownList(this.ddlCollege, "select collegename,collegeid from Dict_College", "collegename", "collegeid");
        this.ddlCollege.Items.Insert(0, new ListItem("全部", "100"));
        this.ddlCollege.SelectedIndex = 0;
    }
    //搜索
    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        this.AspNetPager1.CurrentPageIndex = 0;
        bindGridView(true);
    }
    /// <summary>
    /// 生成sql语句
    /// </summary>
    /// <param name="sqlstrwhere">sql语句</param>
    public void getWhere(ref StringBuilder sqlstrwhere)
    {
        sqlstrwhere.Append("usertype<>4");
        if (this.ddlCollege.SelectedIndex != 0)
            sqlstrwhere.Append(" and depart='").Append(this.ddlCollege.SelectedItem.Text.Trim()).Append("'");
        if (this.ddlLevel.SelectedIndex != 0)
            sqlstrwhere.Append(" and usertype='").Append(this.ddlLevel.SelectedValue.Trim()).Append("'");
        if (this.ddlStatus.SelectedValue != "100")
            sqlstrwhere.Append(" and status='").Append(this.ddlStatus.SelectedValue.Trim()).Append("'");
        if (this.txtDate.Value.Trim() != string.Empty)
            sqlstrwhere.Append(" and CONVERT(varchar(10),regdatetime,121)='").Append(this.txtDate.Value.Trim()).Append("'");
    }

    public void getField(ref StringBuilder sqlstrfield)
    {
        sqlstrfield.Append("id,tid,[name],depart,regdatetime,case status when 1 then '已审核' when 0 then '未审核' when 2 then '临时' else '其他' ");
        sqlstrfield.Append("end as status,case usertype when 1 then '普通用户' when 2 then '学院管理员' ");
        sqlstrfield.Append("when 3 then '项目管理员' else '其他' end as usertype");
    }
}

